﻿using GrainManage.Core.Access.Interfaces;
using GrainManage.Core.Service.Interfaces;
using GrainManage.Core.Util;
using GrainManage.Data.Model;
using GrainManage.Data.Model.Table;
using System.Collections.Generic;
using System.Data;

namespace GrainManage.Core.Service
{
    public class DistrictService : BaseService, IDistrictService
    {
        public DistrictService(IAccess proc) : base(proc) { }

        public DistrictTable Get(int districtID)
        {
            DataSet ds = Proc.ExecuteQuery(ProcConst.District_GetByID, districtID);
            return ConvertToModel<DistrictTable>(GetFirstRow(ds));
        }

        public List<DistrictTable> Search(string name, IPageable page, ICountable records)
        {
            DataSet data = Proc.ExecuteQuery(ProcConst.District_Search, name, page.PageIndex, page.PageSize);
            return ConvertToModel<DistrictTable>(data, records);
        }

        public DistrictTable GetFather(int districtID)
        {
            DataSet ds = Proc.ExecuteQuery(ProcConst.District_GetFatherByID, districtID);
            return ConvertToModel<DistrictTable>(GetFirstRow(ds));
        }

        public List<DistrictTable> GetChild(int districtID, IPageable page, ICountable records)
        {
            var data = Proc.ExecuteQuery(ProcConst.District_GetChildByIDExtent, districtID, page.PageIndex, page.PageSize);
            return ConvertToModel<DistrictTable>(data, records);
        }

        public List<DistrictTable> GetDownwardDistrict(int districtID, int depth, IPageable page, ICountable records)
        {
            DataSet data = Proc.ExecuteQuery(ProcConst.District_GetDownwardByID, districtID, depth, page.PageIndex, page.PageSize);
            return ConvertToModel<DistrictTable>(data, records);
        }

        public List<DistrictTable> GetUpwardDistrict(int districtID, int depth, ICountable records)
        {
            DataSet data = Proc.ExecuteQuery(ProcConst.District_GetUpwardByID, districtID, depth);
            return ConvertToModel<DistrictTable>(data, records);
        }
    }
}
